<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="退货原因" prop="reason">
        <el-select v-model="queryParams.reason" placeholder="请选择退货原因" clearable size="small">
          <el-option
            v-for="dict in reasonOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="退款金额" prop="afterSaleMoney">
        <el-input
          v-model="queryParams.afterSaleMoney"
          placeholder="请输入退款金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退货说明" prop="afterSaleExplain">
        <el-input
          v-model="queryParams.afterSaleExplain"
          placeholder="请输入退货说明"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退货凭证" prop="afterSaleVoucher">
        <el-input
          v-model="queryParams.afterSaleVoucher"
          placeholder="请输入退货凭证"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退货状态" prop="afterSaleState">
        <el-select v-model="queryParams.afterSaleState" placeholder="请选择退货状态" clearable size="small">
          <el-option label="正在退款" value="0"/>
          <el-option label="已退款" value="1"/>
          <el-option label="退款失败" value="2"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['shopping:afterSale:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['shopping:afterSale:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['shopping:afterSale:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['shopping:afterSale:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="afterSaleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="售后表ID" align="center" prop="afterSaleId" />
      <el-table-column label="订单号" align="center" prop="orderMaster.orderSn" />
      <el-table-column label="退款人" align="center" prop="orderMaster.shippingUser" />
      <el-table-column label="商品名" align="center" prop="productInfo.productName" />
      <el-table-column label="退货原因" align="center" prop="reason" :formatter="reasonFormat" />
      <el-table-column label="退款金额" align="center" prop="afterSaleMoney" />
      <el-table-column label="退货说明" align="center" prop="afterSaleExplain" />
      <el-table-column label="退货凭证" align="center" prop="afterSaleVoucher" />
      <el-table-column label="退货状态" align="center" prop="afterSaleState">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.afterSaleState == '0'">正在退款</el-tag>
          <el-tag v-else-if="scope.row.afterSaleState == '1'" type="success">退款成功</el-tag>
          <el-tag v-else-if="scope.row.afterSaleState == '2'" type="danger">退款失败</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="退款时间" align="center" prop="afterSaleTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.afterSaleTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后修改时间" align="center" prop="modifiedTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.modifiedTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.afterSaleState == '0'" size="mini" type="text" icon="el-icon-edit" @click="handleRefund(scope.row)" v-hasPermi="['shopping:afterSale:edit']">确认退款</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

<!--    &lt;!&ndash; 添加或修改售后管理对话框 &ndash;&gt;-->
<!--    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>-->
<!--      <el-form ref="form" :model="form" :rules="rules" label-width="80px">-->
<!--        <el-form-item label="订单表ID" prop="orderMasterId">-->
<!--          <el-input v-model="form.orderMasterId" placeholder="请输入订单表ID" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="用户表ID" prop="customerId">-->
<!--          <el-input v-model="form.customerId" placeholder="请输入用户表ID" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="商品ID" prop="productInfoId">-->
<!--          <el-input v-model="form.productInfoId" placeholder="请输入商品ID" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="退货原因" prop="reason">-->
<!--          <el-select v-model="form.reason" placeholder="请选择退货原因">-->
<!--            <el-option-->
<!--              v-for="dict in reasonOptions"-->
<!--              :key="dict.dictValue"-->
<!--              :label="dict.dictLabel"-->
<!--              :value="dict.dictValue"-->
<!--            ></el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="退款金额" prop="afterSaleMoney">-->
<!--          <el-input v-model="form.afterSaleMoney" placeholder="请输入退款金额" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="退货说明" prop="afterSaleExplain">-->
<!--          <el-input v-model="form.afterSaleExplain" placeholder="请输入退货说明" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="退货凭证" prop="afterSaleVoucher">-->
<!--          <el-input v-model="form.afterSaleVoucher" placeholder="请输入退货凭证" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="退货状态:0表示正在退款,1表示已退款,2表示退款失败" prop="afterSaleState">-->
<!--          <el-select v-model="form.reason" placeholder="请选择退货原因">-->
<!--            <el-option-->
<!--              v-for="dict in reasonOptions"-->
<!--              :key="dict.dictValue"-->
<!--              :label="dict.dictLabel"-->
<!--              :value="dict.dictValue"-->
<!--            ></el-option>-->
<!--          </el-select>-->
<!--          <el-input v-model="form.afterSaleState" placeholder="请输入退货状态:0表示正在退款,1表示已退款,2表示退款失败" />-->
<!--        </el-form-item>-->
<!--      </el-form>-->
<!--      <div slot="footer" class="dialog-footer">-->
<!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
<!--        <el-button @click="cancel">取 消</el-button>-->
<!--      </div>-->
<!--    </el-dialog>-->

  </div>
</template>

<script>
import { listAfterSale, getAfterSale, delAfterSale, addAfterSale, updateAfterSale, exportAfterSale, updateRefund } from "@/api/shopping/afterSale";

export default {
  name: "AfterSale",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 售后管理表格数据
      afterSaleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 退货原因字典
      reasonOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        reason: null,
        afterSaleMoney: null,
        afterSaleExplain: null,
        afterSaleVoucher: null,
        afterSaleState: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderMasterId: [
          { required: true, message: "订单表ID不能为空", trigger: "blur" }
        ],
        customerId: [
          { required: true, message: "用户表ID不能为空", trigger: "blur" }
        ],
        productInfoId: [
          { required: true, message: "商品ID不能为空", trigger: "blur" }
        ],
        reason: [
          { required: true, message: "退货原因不能为空", trigger: "change" }
        ],
        afterSaleMoney: [
          { required: true, message: "退款金额不能为空", trigger: "blur" }
        ],
        afterSaleState: [
          { required: true, message: "退货状态:0表示正在退款,1表示已退款,2表示退款失败不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("after_sale_reason").then(response => {
      this.reasonOptions = response.data;
    });
  },
  methods: {
    /** 查询售后管理列表 */
    getList() {
      this.loading = true;
      listAfterSale(this.queryParams).then(response => {
        this.afterSaleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 退货原因字典翻译
    reasonFormat(row, column) {
      return this.selectDictLabel(this.reasonOptions, row.reason);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        afterSaleId: null,
        orderMasterId: null,
        customerId: null,
        productInfoId: null,
        reason: null,
        afterSaleMoney: null,
        afterSaleExplain: null,
        afterSaleVoucher: null,
        afterSaleState: null,
        orderSn :null,
        productInfo: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.afterSaleId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加售后管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const afterSaleId = row.afterSaleId || this.ids
      getAfterSale(afterSaleId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改售后管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.afterSaleId != null) {
            updateAfterSale(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addAfterSale(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },

    /** 退款操作 */
    handleRefund(row){
      const afterSaleIds = row.afterSaleId || this.ids;
      this.$confirm('正在为编号"' + afterSaleIds + '"的数据项进行退款操作,是否进行确认?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return updateRefund(afterSaleIds,1);
      }).then(() => {
        this.getList();
        this.msgSuccess("退款成功");
      }).catch(function() {});
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const afterSaleIds = row.afterSaleId || this.ids;
      this.$confirm('是否确认删除售后管理编号为"' + afterSaleIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delAfterSale(afterSaleIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有售后管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportAfterSale(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },

  }
};
</script>
